package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import test.learn.LinkExtractor;

/**
 * @author suwy
 * @date 2014-2-14
 * @todo TODO
 */
public class ReadExl {
	
	private static final Logger LOG = Logger.getLogger(ReadExl.class);
	private static String excelPath = "I:\\data\\新康界抓取数据\\third2.xlsx";
	
	private void readExl() {
		File file = new File(excelPath);
		InputStream is = null;
		try {
			is = new FileInputStream(file);
//			POIFSFileSystem fs = new POIFSFileSystem(is);
			XSSFWorkbook xw = new XSSFWorkbook(is);
			XSSFSheet sheet = xw.getSheetAt(0);
			XSSFRow row = null;
			StringBuilder sBuilder = new StringBuilder("A0I0201");//"A010301","A020B0101"
			StringBuilder content = new StringBuilder("\"");
			for (int i = 0; i <= sheet.getLastRowNum(); i++) {
				row = sheet.getRow(i);
				if(sBuilder.toString().equals(row.getCell(1).getStringCellValue())) {
					content.append(row.getCell(0).getStringCellValue() + ",");
				} else {
					content.append("\",\n\"");
					content.append(row.getCell(0).getStringCellValue() + ",");
					sBuilder.delete(0, sBuilder.length());
					sBuilder.append(row.getCell(1).getStringCellValue());
				}
			}
			LOG.info(content.toString().replace(",\",", "\","));
		} catch (FileNotFoundException e) {
			LOG.info(e);
		} catch (IOException e) {
			LOG.info(e);
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				LOG.info(e);
			}
		}
	}

	public static void main(String[] args) {
		ReadExl instance = new ReadExl();
		instance.readExl();
	}

}
